oa.view.rf.compHouse = {
/**
 * Render the structure of an Computation House
 * 
 * @author Gerd Wagner
 * @param [ArithmeticProblems] apl  An arithmetic problem list.
 *
 */
render:  function (apl) {
  var el, rowEl, leftCellEl, rightCellEl;
  var rB = document.getElementById("renderBox");
  var tblBodyEl = document.createElement("tbody");
  var houseEl = dom.createDiv("compHouse");
  rB.appendChild( houseEl);

  el = dom.createDiv("roof");
  houseEl.appendChild(el);
  el = document.createElement("table");
  el.appendChild( tblBodyEl);
  houseEl.appendChild( el);

  for (var i=1; i <= apl.length ; i++) { 
    rowEl = document.createElement("tr");
    rowEl.setAttribute("id", "row"+i);
    tblBodyEl.appendChild( rowEl);
    leftCellEl = document.createElement("td");
    rowEl.appendChild( leftCellEl);
    rightCellEl = document.createElement("td");
    rowEl.appendChild( rightCellEl);
    el = dom.createNumInput("askedFor"+i,"askedFor");
    el.addEventListener("input", oa.view.rf.compHouse.handleUserInputEvents, false);
    if (apl[i-1].type.askedFor === 0) {
      el.setAttribute("name", "result"+i);
      rightCellEl.appendChild( el);
    } else {
      el.setAttribute("name", "op1"+i);
      leftCellEl.appendChild( el);   
    }
  };
  // configure the submit/continue button
  el = dom.createButton("submitButton","blue","?");
  rB.appendChild( el);
  el.addEventListener("click", oa.view.rf.compHouse.handleClickSubmitButtonEvents, false);
  document.addEventListener("keypress", oa.view.handleKeyPressEvents, true);
},
/**
 * Fill an already rendered ArithmeticHouse with new values.
 * 
 * @author Gerd Wagner
 * @param [{ArithmeticProblem}] apl  An arithmetic problem list.
 */
fill: function (apl) {
  // fill the roof with the second operand
  var askedFor = document.getElementById("askedFor1");
  var roofEl = document.getElementById("roof");
  var operatorSymbol = oa.view.operatorSymbols[(apl[0].type.operators[0]-1)];
  roofEl.innerHTML = operatorSymbol + apl[0].operands[1];
  // fill the house/table with first operands or results
  for (var i=0; i < apl.length; i++) { 
    if (apl[i].type.askedFor === 0 ) { 
      document.getElementById("row"+(i+1)).firstChild.innerHTML = apl[i].operands[0]; 
    } else {
      document.getElementById("result"+(i+1)).innerHTML = apl[i].result;  
    }
  }
  //askedFor.value = ""; 
  askedFor.focus();
},
/**
 * Event handler for click submit button events
 * 
 * @author Gerd Wagner
 * @param [event] e  A click event
 *
 */
handleClickSubmitButtonEvents: function (e) {
  var userInputField, answer, i=0,
      givenAnswers = 0,     // not provided by the learner, but by the program on request 
      correctAnswers = 0,   // previously answered correctly
      expectedAnswers = 0,  // = #problems - correctAnswers - givenAnswers
      submitButton = e.target;
  if (submitButton.innerHTML !== "?") {  // Continue button
    oa.ctrl.continueCurrentLearningUnit();
  } else {    
    for (var k=1; k <= oa.currentProblems.length; k++) { 
      problem = oa.currentProblems[k-1];
      userInputField = document.getElementById("askedFor"+k);
      if (userInputField.classList.contains("correct") ||
          userInputField.classList.contains("given")) {
        continue;
      }
      expectedAnswers++;  
      answer = userInputField.value;
      if (answer === "") {
        userInputField.value = problem.getCorrectAnswer();
        userInputField.classList.add("given"); 
        givenAnswers++;
      } else {
        if (parseInt(answer) === problem.getCorrectAnswer()) {
          userInputField.classList.remove("false");
          userInputField.classList.add("correct");
          correctAnswers++;
          if (oa.attemptCounter <= 1) { 
            oa.correctAnswerCounter += 1;
            if (problem.isBasicProblem()) {
              i = problem.operands[0] * 10 + problem.operands[1];  
              oa.currentExercise.getBasicProblemStatistics()[i].correctCounter++;
            }
          }
        } else {
          if (problem.isBasicProblem()) {
            i = problem.operands[0] * 10 + problem.operands[1];
            if (oa.currentExercise.getBasicProblemStatistics()[i].correctCounter > 0) {
              oa.currentExercise.getBasicProblemStatistics()[i].correctCounter--;
            }
          }
          userInputField.classList.add("false");
          userInputField.focus();
        }
      }
    } 
    if (correctAnswers + givenAnswers === expectedAnswers) {
      oa.view.showSmilingAri();
      submitButton.innerHTML = oa.view.continueSymbol;
      submitButton.focus();
    } else { 
      oa.attemptCounter += 1;
      oa.view.showDisappointedAri();      
    }
    percentageCorrect = (oa.learningUnitProblemCounter<=0) ? 0 : 
        Math.floor( oa.correctAnswerCounter / oa.learningUnitProblemCounter * 100);
    oa.view.showPercentageCorrect( percentageCorrect);
  }
},
/**
 * Event handler for reseting the input field's class value on new user input 
 * 
 * @author Gerd Wagner
 */
handleUserInputEvents: function (e) {
  e.target.classList.remove("correct");
  e.target.classList.remove("false");
}
};